package com.fzfnn.scrapbook.controller;

import com.fzfnn.scrapbook.dto.UserDto.*;
import com.fzfnn.scrapbook.service.UserService;

import com.fzfnn.scrapbook.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@Slf4j
@Tag(name = "账户接口", description = "用户账户相关接口")
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public R register(@RequestBody RegisterRequestDto registerRequestDto) {
        return userService.register(registerRequestDto.getEmail(), registerRequestDto.getPassword(), registerRequestDto.getCode());
    }
    @GetMapping("/getCode")
    @Operation(summary = "获取验证码")
    public R getCode(@RequestParam("email") String email) {
       return userService.getCode(email);
    }
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public R login(@RequestBody LoginRequestDto loginRequestDto, HttpServletRequest request) {
        return userService.login(loginRequestDto.getEmail(),loginRequestDto.getPassword(), request);
    }
    @GetMapping("/getCurrentUser")
    @Operation(summary = "获取当前用户信息")
    public R getCurrentUser(@RequestHeader("Authorization") String token) {
        return userService.getCurrentUser(token);
    }
    @GetMapping("/logout")
    @Operation(summary = "用户登出")
    public R logout(@RequestHeader("Authorization") String token) {
        return userService.logout(token);
    }
    @PostMapping("/updateUserInfo")
    @Operation(summary = "更新用户信息")
    public R updateUserInfo(@RequestBody UpdateUserInfoRequestDto  updateUserInfoRequestDto,
            @RequestHeader("Authorization") String token) {
        return userService.updateUserInfo(updateUserInfoRequestDto.getUsername(), updateUserInfoRequestDto.getPassword(), updateUserInfoRequestDto.getSignature(),token);
    }
    @PostMapping("/updateEmail")
    @Operation(summary = "更新邮箱")
    public R updateEmail(@RequestBody UpdateEmailRequestDto updateEmailRequestDto, @RequestHeader("Authorization") String token) {
        return userService.updateEmail(updateEmailRequestDto.getEmail(), updateEmailRequestDto.getCode(), token);
    }

    @PostMapping("/loginbyemail")
    @Operation(summary = "邮箱登录")
    public R loginbyemail(@RequestBody LoginByEmailRequestDto loginbyemailRequestDto) {
        return userService.loginByEmail(loginbyemailRequestDto.getEmail(), loginbyemailRequestDto.getCode());
    }
    @PostMapping("/forgetPassword")
    @Operation(summary = "忘记密码")
    public R forgetPassword(@RequestBody ForgetPasswordRequestDto forgetPasswordRequestDto) {
        return userService.forgetPassword(forgetPasswordRequestDto.getEmail(), forgetPasswordRequestDto.getCode(), forgetPasswordRequestDto.getNewPassword());
    }
    @PostMapping("/auth/refreshToken")
    @Operation(summary = "续期token")
    public R refreshToken(@RequestHeader("Authorization") String authHeader) {
        return userService.parseToken(authHeader);
    }
    @GetMapping("/personalhomepage")
    @Operation(summary = "个人主页")
    public R personalhomepage(@RequestHeader("Authorization") String token, @RequestParam(value = "userId",required = false) Integer userId) {
        return userService.personalHomepage(token, userId);
    }
    @PostMapping("/updateTags")
    @Operation(summary = "更新用户标签")
    public R updateUserTags(@RequestHeader("Authorization") String authHeader) {
        try {
            userService.updateUserTags(authHeader);
            return R.successMsg("用户标签更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.errorMsg("用户标签更新失败");
        }
    }
}
